package com.zzyk.main.configuration.aspectj;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zzyk.main.configuration.exception.UserException;
import com.zzyk.main.model.pojo.Organization;
import com.zzyk.main.model.pojo.RdOrganizationArea;
import com.zzyk.main.model.pojo.RdUserArea;
import com.zzyk.main.model.vo.DeviceInfo;
import com.zzyk.main.model.vo.UserInfo;
import com.zzyk.main.service.*;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Aspect
@Component
@EnableAsync
public class OrgAspect {
    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ISysOrganizationService organizationService;

    @Autowired
    private ISysDeviceService deviceService;
    @Resource
    private RdUserAreaService userAreaService;
    @Resource
    private RdOrganizationAreaService organizationAreaService;

    @Pointcut("execution(public * com.zzyk.*.controller.SysOrganizationController.save(..)) || execution(public * com.zzyk.*.api.controller.ApiCompanyController.save(..))")
    public void orgSavePoint() {
    }

    @Pointcut("execution(public * com.zzyk.*.controller.SysOrganizationController.del(..))")
    public void orgDelPoint() {
    }

    @AfterReturning("orgSavePoint()")
    @Transactional(rollbackFor = Exception.class)
    public void afterAddOrganization(JoinPoint joinPoint) {
        if (joinPoint.getArgs() != null && joinPoint.getArgs()[0] != null && joinPoint.getArgs()[0] instanceof Organization) {
            Organization org = (Organization) joinPoint.getArgs()[0];
          /*  SysUser authUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
            UserInfo aUser = sysUserService.getUser(authUser.getId());*/
            String phone = org.getPhone();
            UserInfo sysUser = sysUserService.getUser(phone);
            List<UserInfo> list = sysUserService.getUserList(null, -1);
//            list.add(aUser);
            list.add(sysUser);
            List<RdUserArea> userAreaList = new ArrayList<>();
            for (UserInfo user : list) {
                RdUserArea userArea = new RdUserArea();
                userArea.setUser(user.getId());
                userArea.setOrganization(org.getId());
                userAreaList.add(userArea);
            }
            userAreaService.saveBatch(userAreaList);
            List<RdOrganizationArea> organizationAreas = new ArrayList<>();
            RdOrganizationArea mitaArea = new RdOrganizationArea();
            mitaArea.setParentId(1);
            mitaArea.setChild(org.getId());
            organizationAreas.add(mitaArea);
           /* RdOrganizationArea oArea = new RdOrganizationArea();
            oArea.setParentId(authUser.getEnterprise());
            oArea.setChild(org.getId());
            organizationAreas.add(oArea);*/
            RdOrganizationArea tArea = new RdOrganizationArea();
            tArea.setParentId(org.getId());
            tArea.setChild(org.getId());
            organizationAreas.add(tArea);
            organizationAreaService.saveBatch(organizationAreas);
        }
    }

    @Before("orgDelPoint()")
    @Transactional(rollbackFor = Exception.class)
    public void beforeDelOrganization(JoinPoint joinPoint) throws UserException {
        if (joinPoint.getArgs() != null && joinPoint.getArgs()[0] != null) {
            int id = (int) joinPoint.getArgs()[0];

            Organization org = organizationService.get(id);
            if (org == null) {
                throw new UserException("企业不存在");
            }

            if (org.getId() == 1) {
                throw new UserException("系统内置企业不可删除");
            }

            List<UserInfo> userList = sysUserService.getUserByOrganizationId(org.getId());
            if (userList != null && userList.size() > 0) {
                throw new UserException("删除企业之前需要先删除其成员");
            }

            List<DeviceInfo> deviceList = deviceService.getDeviceByOrganizationId(org.getId());
            if (deviceList != null && deviceList.size() > 0) {
                throw new UserException("删除企业之前需要先删除其设备");
            }
            userAreaService.remove(new LambdaQueryWrapper<RdUserArea>().eq(RdUserArea::getOrganization, org.getId()));
            organizationAreaService.remove(new LambdaQueryWrapper<RdOrganizationArea>().eq(RdOrganizationArea::getChild, org.getId()));
        }
    }
}
